import { Controller } from 'egg';

export default class ConfigController extends Controller {
  /**
   * @api {get} font/getConfig 获取前台配置信息
   *  
   * @apiName getConfig
   * @apiGroup FontConfig
   * @apiDescription 获取前台配置信息
   *
   */
  public async getConfig() {
    let keysArray = await this.app.redis.hkeys('fontConfigs');
    let obj = {};
    for (let key of keysArray) {
      obj[key] = await this.app.redis.hget('fontConfigs', key);
    }
    this.ctx.response.body = {
      code: 200,
      data: obj,
      status: '成功'
    };
  }

  /**
   * @api {post} font/setConfig 设置前台配置信息
   *  
   * @apiName setConfig
   * @apiGroup FontConfig
   * @apiDescription 设置前台配置信息,采用redis存储，无字段限制
   * 
   */
  public async setConfig() {
    let body = this.ctx.request.body;
    let setFiledsArray: any = [];
    try {
      let keysArray = Object.keys(body);
      if (!keysArray.length) {
        throw this.ctx.helper.getErrorMsg(207);
      }
      for (let key in body) {
        setFiledsArray.push(key);
        setFiledsArray.push(body[key]);
      }
      await this.app.redis.del('fontConfigs');
      await this.app.redis.hmset('fontConfigs', setFiledsArray);
      this.ctx.response.body = {
        code: 200,
        status: '成功'
      };
    } catch (error_info) {
      this.ctx.response.body = error_info;
    }

  }
}
